﻿Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Net
Public Class EMAILFUNIL
    Inherits System.Windows.Forms.Form
    Private m_htmlDoc As mshtml.IHTMLDocument2
    Private m_arAttachment As ArrayList = New ArrayList
    Private m_arCharset(27, 1) As String
    Dim pessoa As String
    Dim assin As String
    Dim i As Integer = 0
    Private Sub EMAIL_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim PESSOA As String
        PESSOA = LoginForm1.NOMEMAILTextBox.Text

        textFrom.Text = LoginForm1.EMAILTextBox.Text
        textUser.Text = LoginForm1.EMAILTextBox.Text
        textPassword.Text = LoginForm1.SENHAEMAILTextBox.Text
        textPassword.Enabled = False
        assin = LoginForm1.ASSINATURATextBox.Text
        textTo.Text = CADASTRO.EMAILTextBox.Text
        textSubject.Text = "Apresentação Vitória Fomento"
        htmlEditor.Navigate("about:blank")
        If DateAndTime.Now.Hour < 12 Then
            Dim TEXTO As String = "Bom dia " & CADASTRO.CONTATOTextBox.Text.ToLower & ",<BR><BR>Conforme nos falamos ao telefone, envio em anexo, a apresentação da Vitória Fomento Mercantil para sua apreciação.<BR><BR>A Vitória Fomento é uma factoring e presta serviço de antecipação de duplicatas e cheques. Nosso intuito é atende-lo em suas demandas de recursos (se for o caso) através de uma parceria, onde visamos o seu crescimento, transformando suas duplicatas e cheques em dinheiro, para que assim, seu caixa esteja sempre à frente dos compromissos. Desta forma, sua empresa sempre terá mais benefícios e descontos por negociar com seus fornecedores à vista.<BR><BR>Há mais de 10 anos no mercado, trabalhamos com taxas competitivas e priorizamos a redução de burocracias hoje exigidas pelos Bancos.<BR><BR>Oferecemos ainda serviço de mensageiro e administração do seu 'contas a pagar' sem nenhum custo adicional, além de direcionar o recurso da operação para quantas contas quiser, assim, sua empresa poderá focar no que realmente é importante, o resultado.<BR><BR><BR>Nos colocamos à disposição para quaisquer esclarecimentos.<BR>Atenciosamente,"
            htmlEditor.Document.Write(TEXTO)
        Else
            Dim TEXTO As String = "Boa Tarde " & CADASTRO.CONTATOTextBox.Text.ToLower & ",<BR><BR>Conforme nos falamos ao telefone, envio em anexo, a apresentação da Vitória Fomento Mercantil para sua apreciação.<BR><BR>A Vitória Fomento é uma factoring e presta serviço de antecipação de duplicatas e cheques. Nosso intuito é atende-lo em suas demandas de recursos (se for o caso) através de uma parceria, onde visamos o seu crescimento, transformando suas duplicatas e cheques em dinheiro, para que assim, seu caixa esteja sempre à frente dos compromissos. Desta forma, sua empresa sempre terá mais benefícios e descontos por negociar com seus fornecedores à vista.<BR><BR>Há mais de 10 anos no mercado, trabalhamos com taxas competitivas e priorizamos a redução de burocracias hoje exigidas pelos Bancos.<BR><BR>Oferecemos ainda serviço de mensageiro e administração do seu 'contas a pagar' sem nenhum custo adicional, além de direcionar o recurso da operação para quantas contas quiser, assim, sua empresa poderá focar no que realmente é importante, o resultado.<BR><BR><BR>Nos colocamos à disposição para quaisquer esclarecimentos.<BR>Atenciosamente,"
            htmlEditor.Document.Write(TEXTO)
        End If
        m_htmlDoc = CType(htmlEditor.Document.DomDocument, mshtml.IHTMLDocument2)
        m_htmlDoc.designMode = "on"
        InitFonts()
        htmlEditor.Focus()
        TextBox1.Text = pessoa
    End Sub
    Private Sub EnviarEMail()
        Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
        message.To.Add(textTo.Text)
        If String.IsNullOrEmpty(textAttachments.Text) Then
        Else
            Dim count As Integer = m_arAttachment.Count
            Dim i As Integer = 0
            For i = 0 To count - 1
                message.Attachments.Add(New System.Net.Mail.Attachment(m_arAttachment(i)))
            Next
        End If
        If CheckBox1.Checked = True Then
            Dim APRESENTACAO As String = "\\SERVIDOR1\Publico Administrativo\Público Administrativo\antigo\Vitória - apresentação digital.pdf"
            message.Attachments.Add(New System.Net.Mail.Attachment(APRESENTACAO))
        End If
        If CheckBox2.Checked = True Then
            Dim APRESENTACAO2 As String = "\\SERVIDOR1\Publico Administrativo\Público Administrativo\antigo\FICHA CADASTRAL.doc"
            message.Attachments.Add(New System.Net.Mail.Attachment(APRESENTACAO2))
        End If
        message.From = New System.Net.Mail.MailAddress(textFrom.Text, TextBox1.Text)
        message.Bcc.Add(textFrom.Text)
        message.Subject = textSubject.Text
        message.IsBodyHtml = True
        If String.IsNullOrEmpty(assin) Then
            message.Body = m_htmlDoc.body.innerHTML
        Else
            'Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(m_htmlDoc.body.innerHTML & "<br><img src=cid:companylogo>", Nothing, "text/plain")
            Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(m_htmlDoc.body.innerHTML & "<br><img src=cid:assinatura>", Nothing, "text/html")
            Dim logo As New LinkedResource(assin)
            logo.ContentId = "assinatura"
            htmlView.LinkedResources.Add(logo)
            message.AlternateViews.Add(htmlView)
            'message.AlternateViews.Add(plainView)
        End If
        Dim smtp As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
        smtp.Host = textServer.Text
        smtp.Port = 587
        If (chkSSL.Checked) Then
            smtp.EnableSsl = True
        Else
            smtp.EnableSsl = False
        End If
        If (chkAuth.Checked) Then
            smtp.Credentials = New System.Net.NetworkCredential(textUser.Text, textPassword.Text)
        End If
        Try
            smtp.Send(message)
            MessageBox.Show("E-mail enviado com sucesso!")
            Cursor.Current = Cursors.Default
        Catch ex As Exception
            MsgBox("Erro ao enviar o email favor verificar os dados" & ex.ToString, MsgBoxStyle.Critical, "Atenção")
        End Try
    End Sub
    Private Sub htmlEditor_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs)
        Dim s As New System.Text.StringBuilder()
        m_htmlDoc.body.innerHTML = s.ToString()
    End Sub
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If textFrom.Text = "" Or textTo.Text = "" Or textUser.Text = "" Or TextBox1.Text = "" Then
            MsgBox("FAVOR VERIFICAR TODOS OS CAMPOS" + Environment.NewLine + "Não se esqueça de preencher o nome")
        Else
            Cursor.Current = Cursors.WaitCursor
            EnviarEMail()
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()

    End Sub
    Private Sub btnClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        m_arAttachment.Clear()
        textAttachments.Text = ""
        htmlEditor.Focus()
    End Sub
    Private Sub lstFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFont.SelectedIndexChanged
        Dim font As String = lstFont.Text
        lstFont.SelectedIndex = 0
        m_htmlDoc.execCommand("fontname", False, font)
        htmlEditor.Focus()
    End Sub
    Private Sub lstSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSize.SelectedIndexChanged
        Dim size As String = lstSize.Text
        lstSize.SelectedIndex = 0
        m_htmlDoc.execCommand("fontsize", False, size)
        htmlEditor.Focus()
    End Sub
    Protected Sub InitFonts()
        Dim arFonts() As String = {"Selecione a Fonte ...", _
                  "Allegro BT", _
                  "Arial", _
                  "Arial Baltic", _
                  "Arial Black", _
                  "Arial CE", _
                  "Arial CYR", _
                  "Arial Greek", _
                  "Arial Narrow", _
                  "Arial TUR", _
                  "AvantGarde Bk BT", _
                  "BankGothic Md BT", _
                  "Basemic", _
                  "Basemic Symbol", _
                  "Basemic Times", _
                  "Batang", _
                  "BatangChe", _
                  "Benguiat Bk BT", _
                  "BernhardFashion BT", _
                  "BernhardMod BT", _
                  "Book Antiqua", _
                  "Bookman Old Style", _
                  "Bremen Bd BT", _
                  "Century Gothic", _
                  "Charlesworth", _
                  "Comic Sans MS", _
                  "CommonBullets", _
                  "CopprplGoth Bd BT", _
                  "Courier", _
                  "Courier New", _
                  "Courier New Baltic", _
                  "Courier New CE", _
                  "Courier New CYR", _
                  "Courier New Greek", _
                  "Courier New TUR", _
                  "Dauphin", _
                  "Dotum", _
                  "DotumChe", _
                  "Dungeon", _
                  "English111 Vivace BT", _
                  "Estrangelo Edessa", _
                  "Fixedsys", _
                  "Franklin Gothic Medium", _
                  "Futura Lt BT", _
                  "Futura Md BT", _
                  "Futura XBlk BT", _
                  "FuturaBlack BT", _
                  "Garamond", _
                  "Gautami", _
                  "Georgia", _
                  "GoudyHandtooled BT", _
                  "GoudyOlSt BT", _
                  "Gulim", _
                  "GulimChe", _
                  "Gungsuh", _
                  "GungsuhChe", _
                  "Haettenschweiler", _
                  "Humanst521 BT", _
                  "Impact", _
                  "Kabel Bk BT", _
                  "Kabel Ult BT", _
                  "Kingsoft Phonetic Plain", _
                  "Latha", _
                  "Lithograph", _
                  "LithographLight", _
                  "Lucida Console", _
                  "Lucida Sans Unicode", _
                  "Mangal", _
                  "Marlett", _
                  "Microsoft Sans Serif", _
                  "MingLiU", _
                  "Modern", _
                  "Monotype Corsiva", _
                  "MS Gothic", _
                  "MS Mincho", _
                  "MS Outlook", _
                  "MS PGothic", _
                  "MS PMincho", _
                  "MS Sans Serif", _
                  "MS Serif", _
                  "MS UI Gothic", _
                  "MT Extra", _
                  "MV Boli", _
                  "Myriad Condensed Web", _
                  "Myriad Web", _
                  "OzHandicraft BT", _
                  "Palatino Linotype", _
                  "PMingLiU", _
                  "PosterBodoni BT", _
                  "Raavi", _
                  "Roman", _
                  "Script", _
                  "Serifa BT", _
                  "Serifa Th BT", _
                  "Shruti", _
                  "Small Fonts", _
                  "Souvenir Lt BT", _
                  "Staccato222 BT", _
                  "Swiss911 XCm BT", _
                  "Sylfaen", _
                  "Symbol", _
                  "System", _
                  "Tahoma", _
                  "Terminal", _
                  "Times New Roman", _
                  "Times New Roman Baltic", _
                  "Times New Roman CE", _
                  "Times New Roman CYR", _
                  "Times New Roman Greek", _
                  "Times New Roman TUR", _
                  "Trebuchet MS", _
                  "Tunga", _
                  "TypoUpright BT", _
                  "Verdana", _
                  "VisualUI", _
                  "Webdings", _
                  "Wingdings", _
                  "Wingdings 2", _
                  "Wingdings 3", _
                  "WP Arabic Sihafa", _
                  "WP ArabicScript Sihafa", _
                  "WP BoxDrawing", _
                  "WP CyrillicA", _
                  "WP CyrillicB", _
                  "WP Greek Century", _
                  "WP Greek Courier", _
                  "WP Greek Helve", _
                  "WP Hebrew David", _
                  "WP IconicSymbolsA", _
                  "WP IconicSymbolsB", _
                  "WP Japanese", _
                  "WP MathA", _
                  "WP MathB", _
                  "WP MathExtendedA", _
                  "WP MathExtendedB", _
                  "WP MultinationalA Courier", _
                  "WP MultinationalA Helve", _
                  "WP MultinationalA Roman", _
                  "WP MultinationalB Courier", _
                  "WP MultinationalB Helve", _
                  "WP MultinationalB Roman", _
                  "WP Phonetic", _
                  "WP TypographicSymbols", _
                  "WST_Czec", _
                  "WST_Engl", _
                  "WST_Fren", _
                  "WST_Germ", _
                  "WST_Ital", _
                  "WST_Span", _
                  "WST_Swed", _
                  "ZapfEllipt BT", _
                  "Zurich Ex BT"}

        Dim i As Integer
        Dim nCount As Integer = arFonts.Length
        For i = 0 To nCount - 1
            lstFont.Items.Add(arFonts(i))
        Next

        lstFont.SelectedIndex = 0
        lstSize.Items.Add("Font Size ... ")
        For i = 1 To 20
            lstSize.Items.Add(i)
        Next
        lstSize.SelectedIndex = 0
    End Sub
    Private Sub btnP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnP.Click
        m_htmlDoc.execCommand("InsertImage", True, Nothing)
    End Sub
    Private Sub _ChangeAuthStatus()
        textUser.Enabled = chkAuth.Checked
        textPassword.Enabled = chkAuth.Checked
    End Sub
    Private Sub chkAuth_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAuth.CheckedChanged
        _ChangeAuthStatus()
    End Sub

    Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        attachmentDlg.Reset()
        attachmentDlg.Multiselect = True
        attachmentDlg.CheckFileExists = True
        attachmentDlg.CheckPathExists = True
        If attachmentDlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        Dim attachments() As String = attachmentDlg.FileNames
        Dim nLen As Integer = attachments.Length
        Dim i As Integer = 0
        For i = 0 To nLen - 1
            m_arAttachment.Add(attachments(i))
            Dim fileName As String = attachments(i)
            Dim pos As Integer = fileName.LastIndexOf("\")
            If pos <> -1 Then
                fileName = fileName.Substring(pos + 1)
            End If
            textAttachments.Text += fileName
            textAttachments.Text += ";"
        Next
    End Sub

    Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
        m_htmlDoc.execCommand("Bold", False, Nothing)
        htmlEditor.Focus()
    End Sub

    Private Sub btnI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnI.Click
        m_htmlDoc.execCommand("Italic", False, Nothing)
        htmlEditor.Focus()
    End Sub

    Private Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
        If colorDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim v As String = String.Format("#{0:x2}{1:x2}{2:x2}", colorDlg.Color.R, _
                 colorDlg.Color.G, _
                 colorDlg.Color.B)
            m_htmlDoc.execCommand("ForeColor", False, v)
        End If
        htmlEditor.Focus()
    End Sub

    Private Sub btnU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnU.Click
        m_htmlDoc.execCommand("Underline", False, Nothing)
        htmlEditor.Focus()
    End Sub
End Class